package com.efuture.collection;
import java.util.ArrayList;
import java.util.Collection;
/**
 * @ClassNAME CollectionOftenUsedApi
 * @Description Collection接口常用api
 * @Author zhaoshanshan
 * @Date 2021/9/5 4:54 下午
 * @Version 1.0
 */
public class CollectionOftenUsedApi {

    public static void main(String[] args) {
        //addElements();
        //getCollectionSize();
        //collectionClear();
        //collectionContains();
        //removeCollectionElement();
        //collectionIsEmpty();
        //collectionToArray();
    }
    /**
     * 添加元素
     */
    private static void addElements(){
        Collection arrayList = new ArrayList();//接口不能new 需要子类实现
        arrayList.add(1);
        arrayList.add("123");
        arrayList.add(true);
    }
    /**
     * 获取集合元素的个数
     */
    private static void getCollectionSize(){
        Collection arrayList = new ArrayList();//接口不能new 需要子类实现
        arrayList.add(1);
        arrayList.add("123");
        arrayList.add(true);
        int size = arrayList.size();
        System.out.println(size);
    }
    /**
     * 清空集合里的元素
     */
    private static void collectionClear(){
        Collection arrayList = new ArrayList();//接口不能new 需要子类实现
        arrayList.add(1);
        arrayList.add("123");
        arrayList.add(true);
        int size = arrayList.size();
        System.out.println(size);
        arrayList.clear();
        size = arrayList.size();
        System.out.println(size);
    }
    /**
     * 判断集合是否包含某个元素
     */
    private static void collectionContains(){
        Collection arrayList = new ArrayList();//接口不能new 需要子类实现
        arrayList.add(1);
        arrayList.add("123");
        arrayList.add(true);
        boolean isContains = arrayList.contains(1);
        System.out.println(isContains);
    }
    /**
     * 删除集合里的某个元素
     */
    private static void removeCollectionElement(){
        Collection arrayList = new ArrayList();//接口不能new 需要子类实现
        arrayList.add(1);
        arrayList.add("123");
        arrayList.add(true);
        boolean isRemove =  arrayList.remove("123");
        System.out.println(isRemove);
    }
    /**
     * 判断集合里的元素是否为空
     */
    private static void collectionIsEmpty(){
        Collection arrayList = new ArrayList();//接口不能new 需要子类实现
        arrayList.add(1);
        arrayList.add("123");
        arrayList.add(true);
        boolean isEmpty =  arrayList.isEmpty();
        System.out.println(isEmpty);
    }
    /**
     * 将集合里的元素转为数组
     */
    private static void collectionToArray(){
        Collection arrayList = new ArrayList();//接口不能new 需要子类实现
        arrayList.add(1);
        arrayList.add("123");
        arrayList.add(true);
        Object[] o = arrayList.toArray();
        for (int i=0;i<o.length;i++){
            System.out.println(o[i]);
        }
    }

}